# THIS SCRIPT COMPUTES SUMMARY STATISTICS (VOTER-VALIDATED) ON PARTICIPATION
# IN THE 2016 AND 2020 GENERAL ELECTION AND PRIMARIES, PRESENTED IN TABLE 6. 
# IT MUST BE RUN USING THE FILES "CCES2016_cleaned.csv" AND 
# "CCES2020_cleaned.csv", AVAILABLE IN THE REPLICATION MATERIALS 
rm(list = ls())
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)
########################################
########################################
########################################
########################################
########################################
# READ IN THE DATA: 2016 CCES. 
# USE THE FILE "CCES2016_cleaned.csv"
data.2016 = read.csv(file.choose())
# OBSERVATIONS ASSIGNED A VOTER-VALIDATION WEIGHT, PRE-ELECTION
data.2016.vv = subset(data.2016, is.na(data.2016$commonweight_vv)=="FALSE")
# OBSERVATIONS ASSIGNED A VOTER-VALIDATION WEIGHT, POST-ELECTION 
data.2016.vvpost = subset(data.2016, is.na(data.2016$commonweight_vv_post)=="FALSE")

# READ IN THE DATA: 2020 CES. 
# USE THE FILE "CCES2020_cleaned.csv"
data.2020 = read.csv(file.choose())
# OBSERVATIONS ASSIGNED A VOTER-VALIDATION WEIGHT, PRE-ELECTION
data.2020.vv = subset(data.2020, is.na(data.2020$vvweight)=="FALSE")
# OBSERVATIONS ASSIGNED A VOTER-VALIDATION WEIGHT, POST-ELECTION  
data.2020.vvpost = subset(data.2020, is.na(data.2020$vvweight_post)=="FALSE")

# DESIGN, 2016 CCES (FOR STATISTICS ABOUT VOTING IN THE PRESIDENTIAL PRIMARIES)
design.2016.primaries = svydesign(ids = ~1, 
                                  weights = ~data.2016.vv$commonweight_vv,
                                  data = data.2016.vv)
# DESIGN, 2016 CCES (FOR STATISTICS ABOUT VOTING IN GENERAL ELECTION) 
design.2016.general = svydesign(ids = ~1, 
                                weights = ~data.2016.vvpost$commonweight_vv_post,
                                data = data.2016.vvpost)
# DESIGN, 2020 CES (FOR STATISTICS ABOUT VOTING IN THE PRESIDENTIAL PRIMARIES)
design.2020.primaries = svydesign(ids = ~1, 
                                  weights = ~data.2020.vv$vvweight,
                                  data = data.2020.vv)
# DESIGN, 2020 CES (FOR STATISTICS ABOUT VOTING IN THE GENERAL ELECTION)
design.2020.general = svydesign(ids = ~1, 
                                weights = ~data.2020.vvpost$vvweight_post,
                                data = data.2020.vvpost)
########################################
########################################
########################################
########################################
########################################
# 2016 PRIMARIES: % MATCHED AMONG PARTISANS THAT VOTED
our.props.2016 = svytable(~ voted.presprimaries2 + programmatic, 
                           design = subset(design.2016.primaries, (republican==1 | democrat==1)))
our.props.2016 
our.props.2016 = prop.table(our.props.2016, 1)
our.props.2016
round(our.props.2016[2, 3], 2) # 0.70
# 2020 PRIMARIES: % MATCHED AMONG PARTISANS THAT VOTED
our.props.2020 = svytable(~ voted.presprimaries2 + programmatic, 
                          design = subset(design.2020.primaries, (republican==1 | democrat==1)))
our.props.2020 
our.props.2020 = prop.table(our.props.2020, 1)
our.props.2020
round(our.props.2020[2, 3], 2) # 0.76

# 2016 GENERAL ELECTION: % MATCHED AMONG PARTISANS THAT VOTED
our.props.2016 = svytable(~ voted.general + programmatic, 
                          design = subset(design.2016.general, (republican==1 | democrat==1)))
our.props.2016
our.props.2016 = prop.table(our.props.2016, 1)
our.props.2016
round(our.props.2016[2, 3], 2) # 0.67
# 2020 GENERAL ELECTION: % MATCHED AMONG PARTISANS THAT VOTED
our.props.2020 = svytable(~ voted.general + programmatic, 
                          design = subset(design.2020.general, (republican==1 | democrat==1)))
our.props.2020 
our.props.2020 = prop.table(our.props.2020, 1)
our.props.2020
round(our.props.2020[2, 3], 2) # 0.75

# 2016 PRIMARIES: % MATCHED AMONG *REPUBLICANS* THAT VOTED 
our.props.2016 = svytable(~ voted.presprimaries2 + programmatic, 
                          design = subset(design.2016.primaries, republican==1))
our.props.2016 
our.props.2016 = prop.table(our.props.2016, 1)
our.props.2016
round(our.props.2016[2, 3], 2) # 0.82
# 2020 PRIMARIES: % MATCHED AMONG *REPUBLICANS* THAT VOTED
our.props.2020 = svytable(~ voted.presprimaries2 + programmatic, 
                          design = subset(design.2020.primaries, republican==1))
our.props.2020 
our.props.2020 = prop.table(our.props.2020, 1)
our.props.2020
round(our.props.2020[2, 3], 2) # 0.87

# 2016 PRIMARIES: % MATCHED AMONG *DEMOCRATS* THAT VOTED
our.props.2016 = svytable(~ voted.presprimaries2 + programmatic, 
                          design = subset(design.2016.primaries, democrat==1))
our.props.2016 
our.props.2016 = prop.table(our.props.2016, 1)
our.props.2016
round(our.props.2016[2, 3], 2) # 0.60
# 2020 PRIMARIES: % MATCHED AMONG *DEMOCRATS* THAT VOTED 
our.props.2020 = svytable(~ voted.presprimaries2 + programmatic, 
                          design = subset(design.2020.primaries, democrat==1))
our.props.2020 
our.props.2020 = prop.table(our.props.2020, 1)
our.props.2020
round(our.props.2020[2, 3], 2) # 0.68

# 2016 GENERAL ELECTION: % MATCHED AMONG *REPUBLICANS* THAT VOTED
our.props.2016 = svytable(~ voted.general + programmatic, 
                          design = subset(design.2016.general, republican==1))
our.props.2016 
our.props.2016 = prop.table(our.props.2016, 1)
our.props.2016
round(our.props.2016[2, 3], 2) # 0.79
# 2020 GENERAL ELECTION: % MATCHED AMONG *REPUBLICANS* THAT VOTED
our.props.2020 = svytable(~ voted.general + programmatic, 
                          design = subset(design.2020.general, republican==1))
our.props.2020 
our.props.2020 = prop.table(our.props.2020, 1)
our.props.2020
round(our.props.2020[2, 3], 2) # 0.84

# 2016 GENERAL ELECTION: % MATCHED AMONG *DEMOCRATS* THAT VOTED
our.props.2016 = svytable(~ voted.general + programmatic, 
                          design = subset(design.2016.general, democrat==1))
our.props.2016 
our.props.2016 = prop.table(our.props.2016, 1)
our.props.2016
round(our.props.2016[2, 3], 2) # 0.56
# 2020 GENERAL ELECTION: % MATCHED AMONG *DEMOCRATS* THAT VOTED
our.props.2020 = svytable(~ voted.general + programmatic, 
                          design = subset(design.2020.general, democrat==1))
our.props.2020 
our.props.2020 = prop.table(our.props.2020, 1)
our.props.2020
round(our.props.2020[2, 3], 2) # 0.66